/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LocalizarOrcamentoInserir.java
 *
 * Created on 13/05/2011, 16:49:46
 */
package localizarInserir;

import Classe.BD;
import Classe.Validacao;
import editar.EditarOrcamentoJuridica;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author gladson
 */
public class LocalizarOrcamentoPJInserir extends Validacao {

    /** Creates new form LocalizarOrcamentoInserir */
    public LocalizarOrcamentoPJInserir() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        txtOpcao.setEnabled(false);
    }
    String selecao;
    String opcao;
    int idcliente = 0;
    int idclientepj = 0;
    int tipoClientePfPj = 0;
    boolean test = false;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cbSelecao = new javax.swing.JComboBox();
        txtOpcao = new javax.swing.JFormattedTextField();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaOrcamento = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtidorc = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jButton2.setText("Sair");
        jButton2.setToolTipText("Sair");
        jButton2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Localizar Orçamento", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel2.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel2.setText("Localizar por:");

        cbSelecao.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "","DATA","RAZAO SOCIAL","CNPJ"}));
cbSelecao.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
cbSelecao.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        cbSelecaoItemStateChanged(evt);
    }
    });

    txtOpcao.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    jButton3.setText("Pesquisar");
    jButton3.setToolTipText("Pesquisar");
    jButton3.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton3ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(txtOpcao, javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                    .addComponent(jLabel2)
                    .addGap(18, 18, 18)
                    .addComponent(cbSelecao, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(18, 18, 18)
            .addComponent(jButton3)
            .addContainerGap(381, Short.MAX_VALUE))
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(cbSelecao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(txtOpcao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton3))
            .addContainerGap())
    );

    tabelaOrcamento.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Data", "Código", "Cliente", "CPF/CNPJ", "Valor Total"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jScrollPane1.setViewportView(tabelaOrcamento);

    jPanel4.setBackground(new java.awt.Color(255, 255, 255));
    jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

    jLabel1.setFont(new java.awt.Font("Arial", 1, 11));
    jLabel1.setText("Código Orçamento");

    txtidorc.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

    jButton1.setText("Editar Orçamento");
    jButton1.setToolTipText("Editar Orçamento");
    jButton1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(txtidorc, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jButton1)
            .addContainerGap(430, Short.MAX_VALUE))
    );
    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(txtidorc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2))
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(jButton2)
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        if (cbSelecao.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Selecione uma opção para consulta!!");
        } else {
            if (validarLocalizador(txtOpcao.getText())) {
                opcao = txtOpcao.getText().trim().toUpperCase();

                if (cbSelecao.getSelectedItem() == "CNPJ") {
                    selecao = "CNPJ_CLIENTE_PJ";
                    test = false;

                } else if (cbSelecao.getSelectedItem() == "DATA") {

                    selecao = "DATA_ORCAMENTO";
                    test = false;

                } else if (cbSelecao.getSelectedItem() == "RAZAO SOCIAL") {
                    selecao = "RAZAO_CLIENTE_PJ LIKE '%";
                    test = true;

                }

                try {
                    String sql = "SELECT O.DATA_ORCAMENTO,O.ID_ORCAMENTO,CJ.RAZAO_CLIENTE_PJ,O.VALOR_TOT_ORCAMENTO,"
                            + "CJ.CNPJ_CLIENTE_PJ,CJ.ID_CLIENTE_PJ FROM ORCAMENTO O "
                            + "INNER JOIN CLIENTE_PJ CJ ON O.ID_CLIENTE_PJ = CJ.ID_CLIENTE_PJ "
                            + "WHERE " + selecao + "='" + opcao + "'";

                    if (test) {
                        String sql2 = "SELECT O.DATA_ORCAMENTO,O.ID_ORCAMENTO,CJ.RAZAO_CLIENTE_PJ,"
                                + "O.VALOR_TOT_ORCAMENTO,CJ.CNPJ_CLIENTE_PJ,CJ.ID_CLIENTE_PJ "
                                + "FROM ORCAMENTO O INNER JOIN CLIENTE_PJ CJ ON O.ID_CLIENTE_PJ = CJ.ID_CLIENTE_PJ "
                                + "WHERE " + selecao + "" + opcao + "%'";

                        sql = sql2;

                    }

                    BD bd = new BD();

                    ResultSet res = bd.consultar(sql);
                    String nome = "";
                    String CPFCNPJ = "";

                    DefaultTableModel model = (DefaultTableModel) tabelaOrcamento.getModel();
                    model.setNumRows(0);

                    while (res.next()) {

                        idclientepj = res.getInt("ID_CLIENTE_PJ");


                        String razao = res.getString("RAZAO_CLIENTE_PJ");

                        String Cnpj = res.getString("CNPJ_CLIENTE_PJ");
                        String data = res.getString("DATA_ORCAMENTO");

                        String[] datar = data.split("-");
                        String[] Hora = data.split(" ");
                        String dia = datar[2].split(" ")[0];
                        String mes = datar[1];
                        String ano = datar[0];
                        data = dia + "/" + mes + "/" + ano;


                        if (razao != null) {
                            nome = razao;
                        }

                        if (Cnpj != null) {
                            CPFCNPJ = Cnpj;
                        }

                        Object[] linha = {
                            data,
                            res.getInt("ID_ORCAMENTO"),
                            nome,
                            CPFCNPJ,
                            res.getDouble("VALOR_TOT_ORCAMENTO")
                        };

                        model.addRow(linha);
                    }


                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(LocalizarOrcamentoPJInserir.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(LocalizarOrcamentoPJInserir.class.getName()).log(Level.SEVERE, null, ex);
                }


            }
        }
    }//GEN-LAST:event_jButton3ActionPerformed
    MaskFormatter mascara = new MaskFormatter();
    private void cbSelecaoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecaoItemStateChanged
        txtOpcao.setEnabled(true);

        if (cbSelecao.getSelectedItem() == "CNPJ") {

            test = false;
            try {
                mascara.setMask("##.###.###/####-##");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(txtOpcao);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarOrcamentoPJInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (cbSelecao.getSelectedItem() == "DATA") {
            try {

                test = true;
                mascara.setMask("##/##/##");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(txtOpcao);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarOrcamentoPJInserir.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (cbSelecao.getSelectedItem() == "RAZAO SOCIAL") {

            test = true;
            mascara.uninstall();
            txtOpcao.setText("");
        }
    }//GEN-LAST:event_cbSelecaoItemStateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed


        if (validarNumero(txtidorc.getText())) {

            try {
                int idorc = Integer.parseInt(txtidorc.getText().trim());
                String sql = "SELECT * FROM ORCAMENTO O WHERE O.ID_ORCAMENTO=" + idorc;
                BD bd = new BD();
                ResultSet rs = bd.consultar(sql);
                if (rs.next()) {
                    EditarOrcamentoJuridica edit = new EditarOrcamentoJuridica();
                    edit.iniciar(idorc);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Orçamento não localizado na base de dados!!!");
                    txtidorc.setText("");
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarOrcamentoInserir.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarOrcamentoInserir.class.getName()).log(Level.SEVERE, null, ex);
            }

        }


    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LocalizarOrcamentoPJInserir().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbSelecao;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaOrcamento;
    private javax.swing.JFormattedTextField txtOpcao;
    private javax.swing.JTextField txtidorc;
    // End of variables declaration//GEN-END:variables
}
